package top.kpromise.user.email;

import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Component;
import top.kpromise.user.crash.CrashHandler;

import javax.mail.internet.MimeMessage;

@Component
public class EmailTools {

    private final JavaMailSender javaMailSender;

    public EmailTools(JavaMailSender javaMailSender) {
        this.javaMailSender = javaMailSender;
    }

    public void sendActiveUserVerifyCode(String email, String verifyCode) {
        MimeMessage message;
        try {
            message = javaMailSender.createMimeMessage();
            MimeMessageHelper helper = new MimeMessageHelper(message, false);
            helper.setFrom("spring-boot@kpromise.top");
            helper.setTo(email);
            helper.setSubject("您的激活验证码");

            helper.setText("您的验证码是: " + verifyCode + " 验证码5分钟内有效", true);
            javaMailSender.send(message);
        } catch (Exception e) {
            e.printStackTrace();
            CrashHandler.reportException(e);
        }
    }
}
